package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.WorkerDO;
import com.golden.procedure.domain.WorkerRoleDO;
import com.golden.procedure.query.WorkerQueryDO;
import com.golden.procedure.query.WorkerRoleQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.IWorkerService;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 工单服务人员及员工信息接口 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.controller <br>
 *       <b>ClassName:</b> WorkerController <br>
 *       <b>Date:</b> 2018年10月23日 上午10:17:53
 */
@RestController
@RequestMapping(value = "/api/worker")
public class WorkerController extends BaseController {

	@Autowired
	IWorkerService workerService;

	public static String encodeWorker(SingleResponse<WorkerDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeRolePage(PageResponse<WorkerRoleDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<WorkerDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "查询员工工作日历", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "calDate", value = "日历（年月）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "employeeID", value = "员工id", required = true, dataType = "Integer") })
	@RequestMapping(value = { "/calendar.json" }, method = RequestMethod.POST)
	public String calendar(@RequestBody QueryRequest<WorkerQueryDO> request) {

		WorkerQueryDO query = request.getData();

		PageResponse<WorkerDO> response = new PageResponse<WorkerDO>();
		ResultDO resultDO = workerService.workerCalendar(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkerDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}

		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询员工", notes = "通过部门岗位筛选员工")
	@ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "部门code", required = true, dataType = "String"),
			@ApiImplicitParam(name = "jobsID", value = "岗位id", required = true, dataType = "Integer") })
	@RequestMapping(value = { "/queryEmployee.json" }, method = RequestMethod.POST)
	public String queryEmployee(@RequestBody QueryRequest<WorkerQueryDO> request) {

		WorkerQueryDO query = request.getData();
		// 不需要查询状态
		query.setIsNeedStatus(BonConstant.STATUS_FALSE);

		PageResponse<WorkerDO> response = new PageResponse<WorkerDO>();
		ResultDO resultDO = workerService.queryEmployee(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkerDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询员工信息", notes = "")
	@ApiImplicitParam(name = "employeeID", value = "员工id", required = true, dataType = "Integer")
	@RequestMapping(value = { "/getEmployee.json" }, method = RequestMethod.POST)
	public String getEmployee(@RequestBody SingleRequest<WorkerQueryDO> request) {

		WorkerQueryDO query = request.getData();
		if (null == query.getEmployeeID() || query.getEmployeeID() == 0) {
			// TODO：默认为当前登录员工
			query.setEmployeeID(9);
		}

		SingleResponse<WorkerDO> response = new SingleResponse<WorkerDO>();
		ResultDO resultDO = workerService.getEmployeeById(query.getEmployeeID());
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((WorkerDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}
		return encodeWorker(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询成员信息及状态", notes = "安排服务人员时，查询员工信息及状态")
	@ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "部门code", required = true, dataType = "String"),
			@ApiImplicitParam(name = "jobsID", value = "岗位id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nameOrTel", value = "员工姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateStr", value = "摄控日期（时间戳）", required = true, dataType = "String") })
	@RequestMapping(value = { "/queryForSchedule.json" }, method = RequestMethod.POST)
	public String queryForSchedule(@RequestBody QueryRequest<WorkerQueryDO> request) {

		WorkerQueryDO query = request.getData();
		query.setIsNeedStatus(BonConstant.STATUS_TRUE); // 需要查询考勤及排班状态

		if (query.getPageRows() > 0) {
			query.setPage(true);
		}

		PageResponse<WorkerDO> response = new PageResponse<WorkerDO>();
		ResultDO resultDO = workerService.queryEmployee(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkerDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询工单服务成员", notes = "查询工单服务成员")
	@ApiImplicitParam(name = "siteSeatID", value = "工位分组id", required = true, dataType = "Integer")
	@RequestMapping(value = { "/query.json" }, method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<WorkerQueryDO> request) {

		WorkerQueryDO query = request.getData();

		PageResponse<WorkerDO> response = new PageResponse<WorkerDO>();
		ResultDO resultDO = workerService.queryBySiteSeat(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkerDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询角色列表", notes = "根据工作查询角色列表")
	@ApiImplicitParam(name = "workType", value = "工作分类", required = true, dataType = "Integer")
	@RequestMapping(value = { "/listRole.json" }, method = RequestMethod.POST)
	public String listRole(@RequestBody QueryRequest<WorkerRoleQueryDO> request) {

		WorkerRoleQueryDO query = request.getData();

		PageResponse<WorkerRoleDO> response = new PageResponse<WorkerRoleDO>();
		ResultDO resultDO = workerService.listRoleByWorkType(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkerRoleDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SYSTEM_ERROR.getMsg());
		}
		return encodeRolePage(response, PLANINTEXT);
	}
}
